package cn.com.winning.ecare.activity;

//暂时不用

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import cn.com.winning.ecare.common.MyApplication;
import cn.com.winning.ecare.common.PullDownView;
import cn.com.winning.ecare.common.PullDownView.OnPullDownListener;
import cn.com.winning.ecare.utils.HTTPGetTool;
import cn.com.winning.ecare.utils.URLUtils;
import cn.com.winning.ecare.widgets.CustomProgressDialog;

/**健康知识明细-停用
 * @author Administrator
 *
 */
public class HealthKnowledgeMX extends Activity implements OnPullDownListener {

	private static final int WHAT_DID_LOAD_DATA = 0;
	private static final int WHAT_DID_REFRESH = 1;
	private static final int WHAT_DID_MORE = 2;

	private HealthKnowledgeMX oThis = this;
	private CustomProgressDialog proDialog;// 加载框
	private Map<String, Object> map_health;
	private Integer count;
	List<Map<String, Object>> list_health = new ArrayList<Map<String, Object>>();

	private ListView mListView;
	private SimpleAdapter mAdapter;

	private PullDownView mPullDownView;
	private List<String> mStrings = new ArrayList<String>();

	private Intent mIntent;
	private String strId, strGJZ;
	MyApplication myApplication;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.health_pulldown);
		myApplication = (MyApplication) getApplication();
		myApplication.addActivity(this);
		setTitle("健康知识");
		// 初始化view
		findView();

		// 初始化界面数据
		GetData();

	}

	private Handler mUIHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			int type = msg.getData().getInt("type");
			switch (type) {
			case -1:
				// proDialog.dismiss();
				Toast.makeText(oThis, msg.getData().getString("msg"),
						Toast.LENGTH_SHORT).show();
				// 刷新
				mPullDownView.notifyDidMore();
				mPullDownView.notifyDidLoad();
				break;
			case 0: {
				// proDialog.dismiss();
				mAdapter.notifyDataSetChanged();
				// 诉它数据加载完毕;
				mPullDownView.notifyDidLoad();
				break;
			}
			case 1: {
				// addData("refresh");
				// mAdapter.notifyDataSetChanged();
				// 告诉它更新完毕
				// mPullDownView.notifyDidRefresh();
				break;
			}

			case 2: {
				// addData();
				mAdapter.notifyDataSetChanged();
				// 告诉它获取更多完毕
				mPullDownView.notifyDidMore();
				break;
			}
			}

		}

	};

	private void loadData() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				List<String> strings = new ArrayList<String>();
				Message msg = mUIHandler.obtainMessage(WHAT_DID_LOAD_DATA);
				msg.obj = strings;
				msg.sendToTarget();
			}
		}).start();
	}

	private void findView() {

		/*
		 * 1.使用PullDownView 2.设置OnPullDownListener 3.从mPullDownView里面获取ListView
		 */
		mPullDownView = (PullDownView) findViewById(R.id.pull_down_view);
		mPullDownView.setOnPullDownListener(this);
		mListView = mPullDownView.getListView();

		// 请求页数
		count = 2;

		// ListView设置适配器
		mAdapter = new SimpleAdapter(this, list_health,
				R.layout.health_pulldown_item, new String[] { "sxnr" },
				new int[] { R.id.tv_xx });
		mListView.setAdapter(mAdapter);

		mPullDownView.enableAutoFetchMore(true, 1);

		mIntent = getIntent();
		if (mIntent != null) {
			strId = mIntent.getStringExtra("strId");
			strGJZ = mIntent.getStringExtra("strGJZ");
		}

	}

	private void GetData() {
		// proDialog = CustomProgressDialog.createDialog(oThis);
		// proDialog.setMessage("数据加载中...");
		// proDialog.show();
		// proDialog.setCanceledOnTouchOutside(false);
		// 这里单开一个线程和后台数据交互
		new Thread(new LoadData()).start();
	}

	private void addData() {
		try {
			Message msg = mUIHandler.obtainMessage();
			Bundle bundle = new Bundle();
			List<NameValuePair> ls = new ArrayList<NameValuePair>();
			// ls.add(new BasicNameValuePair("userid", myApp.getAccount()));
			ls.add(new BasicNameValuePair("page", String.valueOf(count)));
			ls.add(new BasicNameValuePair("lbid", strId));
			ls.add(new BasicNameValuePair("sxmc", strGJZ));
			JSONObject json = HTTPGetTool.getTool().post(
					URLUtils.HOST + URLUtils.HEALTHMX, ls);

			if (json != null) {
				if (!json.toString().equals(
						"{\"msg\":\"系统错误！\",\"success\":\"false\"}")) {
					if (new JSONArray(json.getString("jsonstr")).length() > 0) {
						JSONArray jsonArr = new JSONArray(
								json.getString("jsonstr"));
						// JSONArray jsonArr = json.getJSONArray("orglist");
						JSONObject js = null;
						for (int i = 0; i < jsonArr.length(); i++) {
							map_health = new HashMap<String, Object>();
							js = jsonArr.getJSONObject(i);
							// map_health.put("sxmc", js.getString("sxmc"));
							map_health.put("sxnr", "[" + js.getString("sxmc")
									+ "]" + js.getString("sxnr"));
							list_health.add(map_health);
						}
						bundle.putInt("type", 2);
						bundle.putString("msg", "");
						count++;
					} else {
						bundle.putInt("type", -1);
						bundle.putString("msg", "暂无数据");
					}

				} else {
					bundle.putInt("type", -1);
					bundle.putString("msg", "系统错误");
				}
			} else {
				bundle.putInt("type", -1);
				bundle.putString("msg", "网络连接失败!");
			}
			msg.setData(bundle);
			mUIHandler.sendMessage(msg);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	class LoadData implements Runnable {

		public void run() {
			// TODO Auto-generated method stub
			Looper.prepare();
			iniData();
			Looper.loop();
		}
	}

	private void iniData() {
		try {
			Message msg = mUIHandler.obtainMessage();
			Bundle bundle = new Bundle();
			List<NameValuePair> ls = new ArrayList<NameValuePair>();
			// ls.add(new BasicNameValuePair("userid", myApp.getAccount()));
			ls.add(new BasicNameValuePair("page", "1"));
			ls.add(new BasicNameValuePair("lbid", strId));
			ls.add(new BasicNameValuePair("sxmc", strGJZ));
			JSONObject json = HTTPGetTool.getTool().post(
					URLUtils.HOST + URLUtils.HEALTHMX, ls);

			if (json != null) {
				if (!json.toString().equals(
						"{\"msg\":\"系统错误！\",\"success\":\"false\"}")) {
					if (new JSONArray(json.getString("jsonstr")).length() > 0) {

						JSONArray jsonArr = new JSONArray(
								json.getString("jsonstr"));
						// JSONArray jsonArr = json.getJSONArray("jsonstr");
						JSONObject js = null;
						for (int i = 0; i < jsonArr.length(); i++) {
							map_health = new HashMap<String, Object>();
							js = jsonArr.getJSONObject(i);
							map_health.put("sxmc", js.getString("sxmc"));
							map_health.put("sxnr", "[" + js.getString("sxmc")
									+ "]\n" + js.getString("sxnr"));
							list_health.add(map_health);
						}
						bundle.putInt("type", 0);
						bundle.putString("msg", "");
					}

					else {
						bundle.putInt("type", -1);
						bundle.putString("msg", "暂无数据");
					}

				} else {
					bundle.putInt("type", -1);
					bundle.putString("msg", "系统错误");
				}
			} else {
				bundle.putInt("type", -1);
				bundle.putString("msg", "网络连接失败!");
			}

			msg.setData(bundle);
			mUIHandler.sendMessage(msg);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		new Thread(new Runnable() {

			@Override
			public void run() {
				Bundle bundle = new Bundle();
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				bundle.putInt("type", 1);
				bundle.putString("msg", "");
				Message msg = mUIHandler.obtainMessage();
				msg.setData(bundle);
				mUIHandler.sendMessage(msg);

			}
		}).start();

	}

	@Override
	public void onMore() {
		// addData();

		new Thread(new Runnable() {

			@Override
			public void run() {
				Bundle bundle = new Bundle();
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				addData();
				// bundle.putInt("type", 2);
				// bundle.putString("msg", "");
				// Message msg = mUIHandler.obtainMessage();
				// msg.setData(bundle);
				// mUIHandler.sendMessage(msg);

			}
		}).start();
	}
}
